/******************************************************************************
 * Copyright (C) 2024 EternalChip, Inc.(Gmbh) or its affiliates.
 *
 * All Rights Reserved.
 *
 * @file bsp_key.c
 *
 * @par dependencies
 * - bsp_key.h
 * - stdio.h
 * - stdint.h
 *
 * @author Cuihan
 *
 * @brief Provide the HAL APIs of AHT21 and corresponding opetions.
 *
 * Processing flow:
 *
 * call directly.
 *
 * @version V1.0 2024-12-24
 *
 * @note 1 tab == 4 spaces!
 *
 *****************************************************************************/

//******************************** Includes *********************************//

#include "bsp_led.h"
#include "main.h"
#include "cmsis_os.h"

//*********************Thread_Func **********************//
osThreadId_t led_TaskHandle;
const osThreadAttr_t led_Task_attributes = {
    .name = "led_Task",
    .stack_size = 128 * 4,
    .priority = (osPriority_t)osPriorityNormal+2,
};
//*********************Thread_Func **********************//

//*********************Queue_Handler ********************//
QueueHandle_t led_queue;

//*********************Queue_Handler ********************//

led_status_t led_operate(led_operation_t led_operation)
{
    led_status_t status = LED_OK;

    if (LED_ON == led_operation)
    {
        HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
    }

    if (LED_OFF == led_operation)
    {
        HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
    }

    if (LED_TOGGLE == led_operation)
    {
        HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    }
    return status;
}

void led_task_func(void *argument)
{
    led_status_t led_ret = LED_OK;
    led_operation_t led_value = LED_ON;

    // 创建led队列
    led_queue = xQueueCreate(10, sizeof(led_operation_t));

    if (NULL == led_queue)
    {
        printf("Create led queue failed.\r\n");
    }
    else
    {
        printf("Create led queue successed.\r\n");
    }

    for (;;)
    {
        // 如果队列不为空
        if (led_queue != 0)
        {
            // 接收led_queue中的值，如果接收不到数据，则会一直等到有数据
            if (xQueueReceive(led_queue, &led_value, portMAX_DELAY) == pdTRUE)
            {
                printf("received led_queue value %d\n", led_value);
                led_operate(led_value);
            }
        }
        osDelay(100);
    }
}